#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：8.pymysql-数据删除.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-12 10:44
'''
import pymysql


def delete_info():
    db = pymysql.connect(host="localhost", user="root", password="123456", port=3306, db="tuling_python")
    cursor = db.cursor()

    sql = """
        delete from employee where age > 18;
    """

    try:
        cursor.execute(sql)
        # 通过连接对象进行事务提交
        db.commit()
        print("删除数据成功")
    except Exception as e:
        print("删除数据失败：", e)
        db.rollback() # 插入失败进行事务回滚
    finally:
        cursor.close()
        db.close()

if __name__ == '__main__':
    delete_info()